<template>
  <div>
    <el-form :model="form" inline>
      <el-form-item label="类型名称:">
        <el-input v-model="form.name" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="table">
    <el-table ref="multipleTableRef" :data="tableData" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" />
      <el-table-column label="类型名称" width="180">
        <template #default="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="已关联仓库" width="180">
        <div @click='$router.replace("/home/everyView/warehouseList")'>查看仓库列表</div>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="setTypeName(scope.row)">
            编辑
          </el-button>
          <el-button link type="primary" size="small" @click.prevent="delect(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
  <div>
    <el-dialog v-model="dialogVisible" width="400">
      <span>
        <el-form :model="formSet" label-width="auto" style="max-width: 600px">
          <el-form-item label="类型名称">
            <el-input v-model="formSet.name" />
          </el-form-item>
          <el-form-item>
            <div style="display: flex;justify-content: space-between;width: 60%;margin:0 auto">
              <el-button type="primary" @click="onSubmitSet">保存</el-button>
              <el-button @click="dialogVisible = false; formSet.name = ''">取消</el-button>
            </div>
          </el-form-item>
        </el-form>
      </span>
      <!-- <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">Cancel</el-button>
          <el-button type="primary" @click="dialogVisible = false">
            Confirm
          </el-button>
        </div>
      </template> -->
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import { warehouseTypeSet, warehouseTypePage, warehouseTypeupdate, warehouseDeleteType } from '@/api/index'
import { ElMessage } from 'element-plus'
// do not use same name with ref
const forms = ref({
  pageSize: 10,
  pageNum: 1
})
const form = ref({})
const tableData = ref([])
onBeforeMount(() => {
  getList()
})
const onSubmit = () => {
  console.log('submit!')
  warehouseTypeSet(form.value).then(res => {
    ElMessage.success('保存成功')
    getList()
  })
}
const getList = () => {
  warehouseTypePage(forms.value).then(res => {
    const { data: { data } } = res
    total.value = data.total
    tableData.value = data.list
  })
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  form.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  form.value.pageNum = val
  getList()
}

// table
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
// 编辑
const dialogVisible = ref(false)
const formSet = ref({ name: '', id: '' })
const setTypeName = (row) => {
  formSet.value.name = row.name
  formSet.value.id = row.id
  dialogVisible.value = true
}
const onSubmitSet = () => {
  warehouseTypeupdate(formSet.value).then(res => {
    ElMessage.success('编辑成功')
    dialogVisible.value = false
    getList()
  })
}
// 删
const delect = (row) => {
  warehouseDeleteType({ id: row.id }).then(res => {
    ElMessage.success('删除成功')
    getList()
  })
}
</script>

<style lang='less' scoped></style>
